package net.xo68.destiny.manager.security;

import net.xo68.destiny.contract.domain.user.ISystemUser;
import net.xo68.destiny.contract.enums.user.UserStateEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * 安全用户信息
 * @author wuxie
 * @version 2020-11-7
 */
public class SecurityUser implements UserDetails,ISystemUser, Serializable {

    /**
     * 用户编号
     */
    private Long id;
    /**
     * 账号
     */
    private String account;
    /**
     * 密码
     */
    private String password;
    /**
     * 昵称
     */
    private String nice;
    /**
     * 性别，1-男，0-女
     */
    private Integer sex;
    /**
     * 用户状态，0-锁定，1-启用，2-删除
     */
    private Integer userState;

    private String roles;

    public SecurityUser() {
    }

    public SecurityUser(ISystemUser systemUser) {
        this.id = systemUser.getId();
        this.account=systemUser.getAccount();
        this.password=systemUser.getPassword();
        this.nice=systemUser.getNice();
        this.sex=systemUser.getSex();
        this.userState=systemUser.getUserState();
        this.roles=systemUser.getRoles();
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        Set<String> roleSet = getRoleSet();
        if(roleSet!=null && !roleSet.isEmpty()){
            for (String role : roleSet) {
                SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role);
                grantedAuthorities.add(authority);
            }
        }
        return grantedAuthorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.getAccount();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return !this.userState.equals(UserStateEnum.LOCK.getCode());
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.userState.equals(UserStateEnum.ENABLE.getCode());
    }

    public Set<String> getRoleSet(){
        Set<String> roleSet= new HashSet<>();
        if(StringUtils.isNotEmpty(this.roles)){
            String[] roleArrary=this.roles.split(",");
            for (String role : roleArrary) {
                roleSet.add(role);
            }
        }
        return roleSet;
    }

    @Override
    public Long getId() {
        return id;
    }

    @Override
    public String getAccount() {
        return account;
    }

    @Override
    public String getNice() {
        return nice;
    }

    @Override
    public Integer getSex() {
        return sex;
    }

    @Override
    public Integer getUserState() {
        return userState;
    }

    @Override
    public String getRoles() {
        return roles;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setNice(String nice) {
        this.nice = nice;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public void setUserState(Integer userState) {
        this.userState = userState;
    }

    public void setRoles(String roles) {
        this.roles = roles;
    }
}
